-- https://github.com/ColinKennedy/neovim-ai-plugins
return {
  -- { import = "lazyvim.plugins.extras.ai.supermaven" },
  -- { import = "lazyvim.plugins.extras.ai.copilot" },
  {
    "MeanderingProgrammer/render-markdown.nvim",
    enabled = true,
    opts = {
      file_types = { "markdown", "Avante", "codecompanion" },
      latex = { enabled = true },
    },
    ft = { "markdown", "Avante", "codecompanion" },
  },

  {
    "yetone/avante.nvim",
    cmd = { "AvanteChat", "AvanteEdit", "AvanteAsk" },
    build = "make",
    enabled = true,
    version = false,
    dependencies = {
      {
        "saghen/blink.cmp",
        optional = true,
        dependencies = { "avante.nvim" },
        opts = {
          sources = {
            default = {
              "avante_commands",
              "avante_mentions",
              "avante_files",
            },
            compat = {
              "avante_commands",
              "avante_mentions",
              "avante_files",
            },
            providers = {
              avante_commands = {
                name = "avante_commands",
                module = "blink.compat.source",
                score_offset = 90, -- show at a higher priority than lsp
                opts = {},
              },
              avante_files = {
                name = "avante_commands",
                module = "blink.compat.source",
                score_offset = 100, -- show at a higher priority than lsp
                opts = {},
              },
              avante_mentions = {
                name = "avante_mentions",
                module = "blink.compat.source",
                score_offset = 1000, -- show at a higher priority than lsp
                opts = {},
              },
            },
          },
        },
      },
      "MunifTanjim/nui.nvim",
      -- {
      --   "HakonHarnes/img-clip.nvim",
      --   event = "VeryLazy",
      --   opts = {
      --     default = {
      --       embed_image_as_base64 = false,
      --       prompt_for_file_name = false,
      --       drag_and_drop = {
      --         insert_mode = true,
      --       },
      --     },
      --   },
      -- },
    },
    opts = {
      system_prompt = function()
        local hub = require("mcphub").get_hub_instance()
        return hub and hub:get_active_servers_prompt() or ""
      end,
      custom_tools = function()
        return {
          require("mcphub.extensions.avante").mcp_tool(),
        }
      end,
      provider = "glm",
      -- provider = "claude-code",
      selector = {
        provider = "snacks",
      },
      behaviour = {
        enable_fastapply = true, -- Enable Fast Apply feature
      },
      providers = {
        openrouter = {
          __inherited_from = "openai",
          endpoint = "https://openrouter.ai/api/v1",
          api_key_name = "OPENROUTER_API_KEY",
          model = "openrouter/auto",
        },
        deepseek = {
          __inherited_from = "openai",
          api_key_name = "DEEPSEEK_API_KEY",
          endpoint = "https://api.deepseek.com",
          model = "deepseek-coder",
        },
        google = {
          __inherited_from = "gemini",
          endpoint = "https://gemini.leeapp.dev/v1beta/models",
        },
        wisdom = {
          __inherited_from = "openai",
          api_key_name = "WISDOM_API_KEY",
          endpoint = "https://wisdom-gate.juheapi.com/v1",
          model = "wisdom-ai-claude-sonnet-4",
        },
        vercel = {
          __inherited_from = "openai",
          api_key_name = "VERCEL_API_KEY",
          endpoint = "https://ai-gateway.vercel.sh/v1/chat",
          model = "anthropic/claude-sonnet-4.5",
        },
      },
      rag_service = {
        enabled = false,
        host_mount = os.getenv("HOME") .. "/www/github",
        runner = "docker", -- Runner for the RAG service (can use docker or nix)
        llm = {
          provider = "openai",
          api_key_name = "GLM_API_KEY",
          endpoint = "https://open.bigmodel.cn/api/coding/paas/v4",
          model = "glm-4.6",
          extra = nil,
        },
        embed = {
          provider = "openai",
          api_key_name = "SILICONFLOW_API_KEY",
          endpoint = "https://api.siliconflow.cn/v1",
          embed_model = "BAAI/bge-m3",
          extra = nil,
        },
        docker_extra_args = "", -- Extra arguments to pass to the docker command
      },
    },
    keys = {
      -- {
      --   "<leader>ip",
      --   function()
      --     return vim.bo.filetype == "AvanteInput" and require("avante.clipboard").paste_image()
      --       or require("img-clip").paste_image()
      --   end,
      --   desc = "clip: paste image",
      -- },
    },
  },

  {
    "olimorris/codecompanion.nvim",
    cmd = { "CodeCompanion", "CodeCompanionChat" },
    enabled = false,
    dependencies = {
      {
        "saghen/blink.cmp",
        optional = true,
        dependencies = { "codecompanion.nvim" },
        opts = {
          sources = {
            default = {
              "codecompanion",
            },
          },
        },
      },
      -- "ravitemer/mcphub.nvim",
      {
        "nvim-mini/mini.diff",
        config = function()
          local diff = require("mini.diff")
          diff.setup({
            -- Disabled by default
            source = diff.gen_source.none(),
          })
        end,
      },

      -- {
      --   "HakonHarnes/img-clip.nvim",
      --   event = "VeryLazy",
      --   opts = {
      --     filetypes = {
      --       codecompanion = {
      --         prompt_for_file_name = false,
      --         template = "[Image]($FILE_PATH)",
      --         use_absolute_path = true,
      --       },
      --     },
      --   },
      -- },
    },
    opts = {
      extensions = {
        -- mcphub = {
        --   callback = "mcphub.extensions.codecompanion",
        --   opts = {
        --     show_result_in_chat = true, -- Show mcp tool results in chat
        --     make_vars = true, -- Convert resources to #variables
        --     make_slash_commands = true, -- Add prompts as /slash commands
        --   },
        -- },
      },

      display = {
        action_palette = {
          provider = "snacks",
        },
      },
      strategies = {
        chat = {
          adapter = "glm",
        },
        inline = {
          adapter = "copilot",
        },
        agent = {
          adapter = "wisdom",
        },
        cmd = {
          adapter = "deepseek",
        },
      },
      adapters = {
        acp = {
          claude_code = "claude_code",
          gemini_cli = "gemini_cli",
        },
        http = {
          glm = function()
            return require("codecompanion.adapters").extend("openai_compatible", {
              env = {
                url = "https://open.bigmodel.cn/api/coding/paas/v4",
                api_key = "GLM_API_KEY",
                chat_url = "/chat/completions",
              },
              schema = {
                model = {
                  default = "glm-4.6",
                },
              },
            })
          end,
          gemini = function()
            return require("codecompanion.adapters").extend("gemini", {
              url = "https://gemini.leeapp.dev/v1beta/models/${model}${stream}key=${api_key}",
            })
          end,
          vercel = function()
            return require("codecompanion.adapters").extend("openai_compatible", {
              env = {
                url = "https://ai-gateway.vercel.sh/v1/ai",
                api_key = "VERCEL_API_KEY",
                chat_url = "/chat/completions",
              },
              schema = {
                model = {
                  default = "anthropic/claude-opus-4.1",
                },
              },
            })
          end,
          wisdom = function()
            return require("codecompanion.adapters").extend("openai_compatible", {
              env = {
                url = "https://wisdom-gate.juheapi.com/v1",
                api_key = "WISDOM_API_KEY",
                chat_url = "/chat/completions",
              },
              schema = {
                model = {
                  default = "wisdom-ai-gemini-2.5-flash",
                },
              },
            })
          end,
        },
      },
    },
  },

  {
    "ravitemer/mcphub.nvim",
    -- https://github.com/samanhappy/mcphub
    enabled = true,
    cmd = { "MCPHub" },
    build = "npm install -g mcp-hub@latest",
    opts = {
      auto_approve = true,
      port = 6666,
      config = vim.fn.expand("~/.config/nvim/mcpservers.json"), -- Path to config file in nvim's config directory
      auto_toggle_mcp_servers = true, -- Let LLMs start and stop MCP servers automatically

      -- Optional options
      on_ready = function(hub)
        -- Called when hub is ready
      end,
      on_error = function(err)
        -- Called on errors
      end,
      shutdown_delay = 0, -- Wait 0ms before shutting down server after last client exits
      log = {
        level = vim.log.levels.WARN,
        to_file = false,
        file_path = nil,
        prefix = "MCPHub",
      },
    },
  },
  {
    "coder/claudecode.nvim",
    dependencies = { "folke/snacks.nvim" },
    -- enabled = false,
    config = true,
    cmd = { "ClaudeCode" },
    keys = {
      -- { "<leader>a", nil, desc = "AI/Claude Code" },
      { "<leader>ac", "<cmd>ClaudeCode<cr>", desc = "Toggle Claude" },
      { "<leader>af", "<cmd>ClaudeCodeFocus<cr>", desc = "Focus Claude" },
      { "<leader>ar", "<cmd>ClaudeCode --resume<cr>", desc = "Resume Claude" },
      { "<leader>aC", "<cmd>ClaudeCode --continue<cr>", desc = "Continue Claude" },
      { "<leader>am", "<cmd>ClaudeCodeSelectModel<cr>", desc = "Select Claude model" },
      { "<leader>ab", "<cmd>ClaudeCodeAdd %<cr>", desc = "Add current buffer" },
      { "<leader>as", "<cmd>ClaudeCodeSend<cr>", mode = "v", desc = "Send to Claude" },
      {
        "<leader>as",
        "<cmd>ClaudeCodeTreeAdd<cr>",
        desc = "Add file",
        ft = { "NvimTree", "neo-tree", "oil", "minifiles" },
      },
      -- Diff management
      { "<leader>aa", "<cmd>ClaudeCodeDiffAccept<cr>", desc = "Accept diff" },
      { "<leader>ad", "<cmd>ClaudeCodeDiffDeny<cr>", desc = "Deny diff" },
    },
  },
  -- {
  --   "folke/sidekick.nvim",
  --   enabled = false,
  --   opts = {
  --     -- add any options here
  --     cli = {
  --       mux = {
  --         backend = "zellij",
  --         enabled = true,
  --       },
  --     },
  --   },
  --   keys = {
  --     {
  --       "<tab>",
  --       function()
  --         -- if there is a next edit, jump to it, otherwise apply it if any
  --         if not require("sidekick").nes_jump_or_apply() then
  --           return "<Tab>" -- fallback to normal tab
  --         end
  --       end,
  --       expr = true,
  --       mode = { "n" },
  --       desc = "Goto/Apply Next Edit Suggestion",
  --     },
  --     {
  --       "<c-.>",
  --       function()
  --         require("sidekick.cli").toggle()
  --       end,
  --       desc = "Sidekick Toggle",
  --       mode = { "n", "t", "i", "x" },
  --     },
  --     {
  --       "<leader>aa",
  --       function()
  --         require("sidekick.cli").toggle()
  --       end,
  --       desc = "Sidekick Toggle CLI",
  --     },
  --     {
  --       "<leader>as",
  --       function()
  --         require("sidekick.cli").select()
  --       end,
  --       -- Or to select only installed tools:
  --       -- require("sidekick.cli").select({ filter = { installed = true } })
  --       desc = "Select CLI",
  --     },
  --     {
  --       "<leader>at",
  --       function()
  --         require("sidekick.cli").send({ msg = "{this}" })
  --       end,
  --       mode = { "x", "n" },
  --       desc = "Send This",
  --     },
  --     {
  --       "<leader>af",
  --       function()
  --         require("sidekick.cli").send({ msg = "{file}" })
  --       end,
  --       desc = "Send File",
  --     },
  --     {
  --       "<leader>av",
  --       function()
  --         require("sidekick.cli").send({ msg = "{selection}" })
  --       end,
  --       mode = { "x" },
  --       desc = "Send Visual Selection",
  --     },
  --     {
  --       "<leader>ap",
  --       function()
  --         require("sidekick.cli").prompt()
  --       end,
  --       mode = { "n", "x" },
  --       desc = "Sidekick Select Prompt",
  --     },
  --     -- Example of a keybinding to open Claude directly
  --     {
  --       "<leader>ac",
  --       function()
  --         require("sidekick.cli").toggle({ name = "claude", focus = true })
  --       end,
  --       desc = "Sidekick Toggle Claude",
  --     },
  --   },
  -- },
}
